package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.entity.BlogTagExample;
import com.qinge.blog.service.BlogContentTagService;
import com.qinge.blog.utils.BeanMapper;
import com.qinge.blog.vo.BlogTagVO;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.dao.BlogTagDao;
import com.qinge.blog.service.BlogTagService;
import com.qinge.blog.entity.BlogTag;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogTag service层实现类
 */
@Service
public class BlogTagServiceImpl  implements BlogTagService {

    @Resource
    BlogTagDao tagDao;

    @Resource
    BlogContentTagService contentTagService;

    @Override
    public void add(BlogTag... blogTags) {
        tagDao.batchInsert(Arrays.asList(blogTags));
    }

    @Override
    public void addByList(List<BlogTag> blogTags) {
        tagDao.batchInsert(blogTags);
    }

    @Override
    public void removeById(String id) {
        tagDao.deleteByPrimaryKey(id);
    }

    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogTagExample example=new BlogTagExample();
        BlogTagExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        tagDao.deleteByExample(example);
    }

    @Override
    public void update(BlogTag blogTag) {
        tagDao.upsert(blogTag);
    }

    @Override
    public void upsert(BlogTag blogTag) {
        tagDao.upsert(blogTag);
    }

    @Override
    public BlogTag findById(String id) {
        return tagDao.selectByPrimaryKey(id);
    }

    @Override
    public BlogTag findByTagName(String tagName) {
        BlogTagExample example=new BlogTagExample();
        BlogTagExample.Criteria criteria=example.createCriteria();

        criteria.andTagNameEqualTo(tagName);

        List<BlogTag> blogTags = tagDao.selectByExample(example);

        if(blogTags.size()==0){
            return null;
        }else{
            return blogTags.get(0);
        }
    }

    @Override
    public List<BlogTag> findByIds(List<String> ids) {
        BlogTagExample example=new BlogTagExample();
        BlogTagExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        return tagDao.selectByExample(example);
    }

    @Override
    public List<BlogTag> findAll() {
        BlogTagExample example=new BlogTagExample();

        List<BlogTag> list=tagDao.selectByExample(example);

        return list;
    }

    @Override
    public PageInfo<BlogTagVO> findAll(PagingReqVO<BlogTag> pagingReqVO) {
        return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogTagVO> paging(PagingReqVO<BlogTag> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogTagExample example=new BlogTagExample();

        List<BlogTag> list=tagDao.selectByExample(example);



        List<BlogTagVO> newList=BeanMapper.mapList(list,BlogTagVO.class);

        newList.stream().forEach(each->each.setCount(contentTagService.findCountByTag(each.getId())));

        return new PageInfo<>(newList);
    }
}

